import type { PageResponse } from './warehouse';

/**
 * 库存盘点相关API
 */
import type { InventoryApi } from '#/models/inventory/inventory';

import { requestClient } from '#/api/request';

/**
 * 盘点单查询参数接口
 */
export interface InventoryCheckQueryParams {
  page?: number;
  pageSize?: number;
  check_code?: string;
  warehouse_id?: number;
  status?: string;
  checker_id?: number;
  date_from?: string;
  date_to?: string;
  [key: string]: any;
}

/**
 * 盘点明细查询参数接口
 */
export interface InventoryCheckDetailQueryParams {
  page?: number;
  pageSize?: number;
  check_id?: number;
  product_id?: number;
  location_id?: number;
  has_variance?: boolean;
  [key: string]: any;
}

/**
 * 创建盘点单参数接口
 */
export interface CreateInventoryCheckParams {
  warehouse_id: number;
  check_date?: string;
  checker_id?: number;
  note?: string;
}

/**
 * 更新盘点明细参数接口
 */
export interface UpdateInventoryCheckDetailParams {
  id: number;
  actual_quantity: number;
  note?: string;
}

/**
 * 获取盘点单列表
 * @param params - 查询参数，支持分页、过滤和排序
 * @returns 分页的盘点单列表数据
 */
export const getInventoryCheckList = (
  params?: InventoryCheckQueryParams,
): Promise<PageResponse<InventoryApi.InventoryCheck>> => {
  return requestClient.get('/inventory/inventory-check/', { params });
};

/**
 * 获取单个盘点单详情
 * @param checkId - 盘点单ID
 * @returns 盘点单详情
 */
export const getInventoryCheck = (
  checkId: number,
): Promise<InventoryApi.InventoryCheck> =>
  requestClient.get(`/inventory/inventory-check/${checkId}/`);

/**
 * 创建盘点单
 * @param data - 盘点单数据
 * @returns 创建成功的盘点单信息
 */
export const createInventoryCheck = (
  data: CreateInventoryCheckParams,
): Promise<InventoryApi.InventoryCheck> =>
  requestClient.post('/inventory/inventory-check/', data);

/**
 * 更新盘点单
 * @param checkId - 盘点单ID
 * @param data - 更新的盘点单信息
 * @returns 更新后的盘点单信息
 */
export const updateInventoryCheck = (
  checkId: number,
  data: Partial<InventoryApi.InventoryCheck>,
): Promise<InventoryApi.InventoryCheck> =>
  requestClient.put(`/inventory/inventory-check/${checkId}/`, data);

/**
 * 删除盘点单
 * @param checkId - 盘点单ID
 * @returns 删除结果
 */
export const deleteInventoryCheck = (checkId: number): Promise<any> => {
  return requestClient.delete(`/inventory/inventory-check/${checkId}/`);
};

/**
 * 开始盘点
 * @param checkId - 盘点单ID
 * @returns 操作结果
 */
export const startInventoryCheck = (checkId: number): Promise<any> => {
  return requestClient.post(
    `/inventory/inventory-check/${checkId}/start-check/`,
  );
};

/**
 * 完成盘点
 * @param checkId - 盘点单ID
 * @returns 操作结果
 */
export const completeInventoryCheck = (
  checkId: number,
): Promise<{
  total_variance_amount: number;
  total_variance_quantity: number;
}> => {
  return requestClient.post(
    `/inventory/inventory-check/${checkId}/complete-check/`,
  );
};

/**
 * 获取盘点明细列表
 * @param params - 查询参数，支持分页、过滤和排序
 * @returns 分页的盘点明细列表数据
 */
export const getInventoryCheckDetailList = (
  params?: InventoryCheckDetailQueryParams,
): Promise<PageResponse<InventoryApi.InventoryCheckDetail>> => {
  return requestClient.get('/inventory/inventory-check-detail/', { params });
};

/**
 * 创建盘点明细
 * @param data - 盘点明细数据
 * @returns 创建成功的盘点明细信息
 */
export const createInventoryCheckDetail = (
  data: Partial<InventoryApi.InventoryCheckDetail>,
): Promise<InventoryApi.InventoryCheckDetail> =>
  requestClient.post('/inventory/inventory-check-detail/', data);

/**
 * 更新盘点明细
 * @param detailId - 盘点明细ID
 * @param data - 更新的盘点明细信息
 * @returns 更新后的盘点明细信息
 */
export const updateInventoryCheckDetail = (
  detailId: number,
  data: Partial<InventoryApi.InventoryCheckDetail>,
): Promise<InventoryApi.InventoryCheckDetail> =>
  requestClient.put(`/inventory/inventory-check-detail/${detailId}/`, data);

/**
 * 删除盘点明细
 * @param detailId - 盘点明细ID
 * @returns 删除结果
 */
export const deleteInventoryCheckDetail = (detailId: number): Promise<any> => {
  return requestClient.delete(`/inventory/inventory-check-detail/${detailId}/`);
};

/**
 * 批量更新盘点明细
 * @param details - 盘点明细数组
 * @returns 更新结果
 */
export const batchUpdateInventoryCheckDetails = (
  details: UpdateInventoryCheckDetailParams[],
): Promise<any> => {
  return requestClient.post('/inventory/inventory-check-detail/batch-update/', {
    details,
  });
};

/**
 * 导出盘点单数据
 * @param checkId - 盘点单ID
 * @returns 导出结果
 */
export const exportInventoryCheckData = (checkId: number): Promise<Blob> => {
  return requestClient.get(`/inventory/inventory-check/${checkId}/export/`, {
    responseType: 'blob',
  });
};
